package com.admin.scroller.photoorvideo.utils;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.Drawable;

import java.lang.reflect.Field;

/**
 * 加载R文件里面的内容
 */
public class Res
{

    // 文件路径
    private static String pkgName;

    private static Resources resources;

    private static Context mContext;
    // 初始化文件夹路径和R资源
    public static void init(Context context)
    {
        mContext=context;
        pkgName = context.getPackageName();
        resources = context.getResources();
    }

    /**
     * layout文件夹下的xml文件id获取
     */
    public static int getLayoutID(String layoutName)
    {
        return resources.getIdentifier(layoutName, "layout", pkgName);
    }

    // 获取到控件的ID
    public static int getWidgetID(String widgetName)
    {
        return resources.getIdentifier(widgetName, "id", pkgName);
    }

    /**
     * anim文件夹下的xml文件id获取
     */
    public static int getAnimID(String animName)
    {
        return resources.getIdentifier(animName, "anim", pkgName);
    }

    /**
     * xml文件夹下id获取
     */
    public static int getXmlID(String xmlName)
    {
        return resources.getIdentifier(xmlName, "xml", pkgName);
    }

    // 获取xml文件
    public static XmlResourceParser getXml(String xmlName)
    {
        int xmlId = getXmlID(xmlName);
        return (XmlResourceParser) resources.getXml(xmlId);
    }

    /**
     * raw文件夹下id获取
     */
    public static int getRawID(String rawName)
    {
        return resources.getIdentifier(rawName, "raw", pkgName);
    }

    /**
     * drawable文件夹下文件的id
     */
    public static int getDrawableID(String drawName)
    {
        return resources.getIdentifier(drawName, "drawable", pkgName);
    }
    /**
     * mipmap文件夹下文件的id
     */
    public static int getMipMapID(String drawName)
    {
        return resources.getIdentifier(drawName, "mipmap", pkgName);
    }

    // 获取到Drawable文件
    public static Drawable getDrawable(String drawName)
    {
        int drawId = getDrawableID(drawName);
        return resources.getDrawable(drawId);
    }

    /**
     * value文件�?
     */
    // 获取到value文件夹下的attr.xml里的元素的id
    public static int getAttrID(String attrName)
    {
        return resources.getIdentifier(attrName, "attr", pkgName);
    }

    // 获取到dimen.xml文件里的元素的id
    public static int getDimenID(String dimenName)
    {
        return resources.getIdentifier(dimenName, "dimen", pkgName);
    }
    // 获取到dimen.xml文件里的元素的id
    public static int getDimensID(String dimenName)
    {
        return resources.getIdentifier(dimenName, "dimens", pkgName);
    }

    // 获取到color.xml文件里的元素的id
    public static int getColorID(String colorName)
    {
        return resources.getIdentifier(colorName, "color", pkgName);
    }

    // 获取到color.xml文件里的元素的id
    public static int getColor(String colorName)
    {
        return resources.getColor(getColorID(colorName));
    }

    // 获取到style.xml文件里的元素id
    public static int getStyleID(String styleName)
    {
        return resources.getIdentifier(styleName, "style", pkgName);
    }

    // 获取到String.xml文件里的元素id
    public static int getStringID(String strName)
    {
        return resources.getIdentifier(strName, "string", pkgName);
    }

    // 获取到String.xml文件里的元素
    public static String getString(String strName)
    {
        int strId = getStringID(strName);
        return resources.getString(strId);
    }

    // 获取color.xml文件里的integer-array元素
    public static int[] getInteger(String strName)
    {
        return resources.getIntArray(resources.getIdentifier(strName, "array",
                pkgName));
    }
    // 获取strings.xml文件里的string-array元素
    public static String[] getStringArray(String strName)
    {
        return resources.getStringArray(resources.getIdentifier(strName, "array",
                pkgName));
    }
    // 通过反射实现
    public static final int[] getStyleableIntArray(String name) {
        try {
            if (mContext == null)
                return null;
            Field field = Class.forName(mContext.getPackageName() + ".R$styleable").getDeclaredField(name);
            int[] ret = (int[]) field.get(null);
            return ret;
        } catch (Throwable t) {
        }
        return null;
    }

    public static final int getStyleableIntArrayIndex(String name) {
        try {
            if (mContext == null)
                return 0;
            // use reflection to access the resource class
            Field field = Class.forName(mContext.getPackageName() + ".R$styleable").getDeclaredField(name);
            int ret = (Integer) field.get(null);
            return ret;
        } catch (Throwable t) {
        }
        return 0;
    }

}
